'''
Author: @xiaonancheng
Date: 2023-02-27 14:42:50
https://www.cnblogs.com/xiaonancheng
Copyright (c) 2023 by @xiaonancheng, All Rights Reserved. 
'''
from django.shortcuts import render,redirect
from .models import Manager
from DoctorApp.models import Doctor
from BlogApp.models import News,Post,Comment
from UserApp.models import User

from django.http import HttpResponseRedirect,JsonResponse
def login_role_auth(fn):
    def wrapper(request,*args,**kwargs):
        if request.session.get('is_login', False) and request.session.get('user_type')=="/admin_sys" :
            return fn(request,*args,*kwargs)
        else:
            # 获取用户当前访问的url，并传递给/user/login/
            next = request.get_full_path() 
            request.session["is_login"]=False
            red = HttpResponseRedirect("/login/")
            return red
    return wrapper

@login_role_auth
def change_pwd(request):#个人中心 修改密码
    massage={"info":''}
    if request.method=="POST":
        old_pwd=request.POST.get("old_pwd")
        new_pwd=request.POST.get("new_pwd")
        if old_pwd==new_pwd:
            massage={"info":'新密码和原密码一致',"res":0}
        else:
            user=Manager.objects.filter(uid=request.session.get('uid')).first()
            if user.password!=old_pwd:
                massage={"info":'原密码不正确',"res":1}
            else:
                user.password=new_pwd
                user.save()
                massage={"info":'密码修改成功',"res":2}
        return JsonResponse(massage) 
    else:
        return render(request,"admin_pwd.html",massage)
def personal_info(request):#个人中心 个人信息
    if request.method=="GET":
        manager=Manager.objects.filter(uid=request.session.get('uid')).first()
        return render(request,"admin_index.html",{"user":manager})
    elif request.method=="POST":
        manager=Manager.objects.filter(uid=request.session.get('uid')).first()
        manager_name=request.POST.get('manager_name')
        email=request.POST.get('email')
        manager_sex=request.POST.get('manager_sex')
        manager_age=request.POST.get('manager_age')
        manager.manager_name=manager_name
        manager.email=email
        manager.manager_sex=manager_sex
        manager.manager_age=manager_age
        manager.save()
        return redirect("/admin_sys/index/")

@login_role_auth
def user_manage(request):#用户管理
    user_id=request.GET.get("uid")
    if request.method=="GET" and not user_id:
        users=User.objects.all()
        return render(request,"patient_manage.html",{"users":users})
    elif user_id:
        user=User.objects.filter(uid=user_id).first()
        user.states=0
        user.save()
        return redirect("/admin_sys/user_manage/")
    if request.method=="POST":
        user_id=request.POST


@login_role_auth
def doctor_manage(request):#医生管理 增删查改
    uid=request.GET.get("uid")
    if request.method=="GET" and not uid:
        doctors=Doctor.objects.all()
        return render(request,"doctor_manage.html",{"doctors":doctors})
    elif uid:
        type=request.GET.get("type")
        doctor=Doctor.objects.filter(uid=uid).first()
        doctor.states=int(type)
        doctor.save()
        return redirect("/admin_sys/doctor_manage/")
    elif request.method=="POST":
        type=request.POST.get("type")
        if type=="add":
            pass
        elif type=="change":
            pass
        elif type=="delete":
            pass
@login_role_auth
def health_news(request):#健康咨询管理
    t=request.GET.get('t')
    news_id=request.GET.get('id')
    if request.method=="GET" and not t:
        news=News.objects.all()
        return render(request,"news_manage.html",{"news":news})
    elif request.method=="GET" and t=='show':
        news=News.objects.filter(news_id=news_id).first()
        return render(request,"news_details-manage.html",{"news":news})
    elif request.method=="GET" and t=='delete':
        news=News.objects.filter(news_id=news_id).first()
        news.delete()

        return redirect("/admin_sys/health_news/")

    elif request.method=="POST":
        type=request.POST.get("type")
        if type=="add":
            news=News()
            news.news_img=request.POST.get("news_img",'/static/img/news-1.jpg')
            news.news_content=request.POST.get("news_content")
            news.news_title=request.POST.get("news_title")
            news.save()
        elif type=="change":
            news_id=request.POST.get("news_id")
            news=News.objects.filter(news_id=news_id).first()
            news.news_img=request.POST.get("news_img",'/static/img/news-1.jpg')
            news.news_content=request.POST.get("news_content")
            news.news_title=request.POST.get("news_title")
            news.save()
        return redirect("/admin_sys/health_news/")
        
@login_role_auth
def blog_manage(request):#论坛管理
    pid=request.GET.get("pid")
    if request.method=="GET" and not pid:
        posts=Post.objects.all()
        return render(request,"discuss_manage.html",{"posts":posts})
    elif pid:
        type=request.GET.get("type")
        if type=="delete":
            post=Post.objects.filter(post_id=pid).first()
            post.delete()
            return redirect("/admin_sys/discuss_manage/")
        elif type=="show":
            post=Post.objects.filter(post_id=pid).first()
            # post.delete()
            comments=Comment.objects.filter(p_id=post)
            return render(request,"post-manage.html",{"post":post,'comments':comments})
    # elif request.method=="POST":
    #     type=request.POST.get("type")
    #     if type=="add":
    #         pass
    #     elif type=="change":
    #         pass
    #     elif type=="delete":
    #         pass
@login_role_auth
def rights_manage(request):#权限管理 查 审核 删除 0申请1通过2不通过 
    if request.method=="GET":
        users=User.objects.all()
        doctors=Doctor.objects.all()
        return render(request,"blog_manage.html",{"users":users,'doctors':doctors})
    elif request.method=="POST":
        type=request.POST.get("type")
        if type=="agree":#通过
            pass
        elif type=="disagree":#不通过
            pass
        elif type=="delete":
            pass
    pass
